import { Injectable, Inject } from '@nestjs/common';
import { Repository } from 'typeorm';
import { User } from '../model/entity/user.entity';

@Injectable()
export class UserService {
  constructor(
    @Inject('USER_REPOSITORY')
    private readonly userRepository: Repository<User>
  ) { }
  /**
   * 函数说明：添加用户数据
   *
   * @param {User} user
   * @return {*}  {Promise<User>}
   * @memberof UserService
   */
  async createUser(user: User): Promise<User> {
    // 插入数据时，清空id，以避免误传id
    delete user.id;
    return await this.userRepository.save(user)
  }

    /**
   *功能说明：根据id删除用户
   *
   * @param {number} id
   * @return {*}  {Promise<void>}
   * @memberof UserService
   */
   async deleteUser(id: number): Promise<Object> {
    return await this.userRepository.delete(id);
  }

  /**
   * 功能说明：更新用户数据
   * @param {number} id
   * @param {User} user
   * @return {*}  {Promise<void>}
   * @memberof UserService
   */
   async updateUser(id: number, user: User): Promise<void> {

    // 插入数据时，清空id，以避免误传id
    delete user.id;
    this.userRepository.update(id, user);
  }

  /**
   * 功能说明：查询符合条件的用户信息
   *
   * @param {User} user
   * @return {*}  {Promise<User[]>}
   * @memberof UserService
   */
  async findByUser(user: User): Promise<User[]> {
    return await this.userRepository.find();
  }

  /**
   * 功能说明：根据ID查询某个用户的信息
   *
   * @param {number} id
   * @return {*}  {Promise<User>}
   * @memberof UserService
   */
  async findById(id: number): Promise<User> {
    const usrinfo = await this.userRepository.findOne(id);
    return usrinfo;
  }
}
